package me.zhanshi123.vipsystem.feature.vipjoin;

import cn.nukkit.Player;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.player.PlayerJoinEvent;
import cn.nukkit.event.player.PlayerQuitEvent;
import cn.nukkit.utils.ConfigSection;
import me.zhanshi123.vipsystem.Main;
import me.zhanshi123.vipsystem.feature.VipFeature;

import java.util.Arrays;

public class VipJoin extends VipFeature implements Listener {
    public VipJoin() {
        super("join");
        Main.getInstance().getServer().getPluginManager().registerEvents(this, Main.getInstance());
        vipJoinGroup = new VipJoinGroup(getConfigSection());
    }

    private VipJoinGroup vipJoinGroup;

    @Override
    public ConfigSection generateDefaultConfig(ConfigSection configSection) {
        configSection.set("vip1.join", Arrays.asList(
                "[Broadcast] &aVIP1玩家{0}加入了游戏!",
                "[Message] &a尊敬的VIP1，欢迎回到游戏，已经自动为您打开飞行模式!",
                "[Console] fly {0}"));
        configSection.set("vip1.leave", Arrays.asList("[Broadcast] &aVIP1玩家{0}离开了游戏!"));
        return configSection;
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player player = e.getPlayer();
        VipJoinAction vipJoinAction = vipJoinGroup.getGroup(player);
        if (vipJoinAction == null) {
            return;
        }
        vipJoinAction.performJoinAction(player);
    }

    @EventHandler
    public void onLeave(PlayerQuitEvent e) {
        Player player = e.getPlayer();
        VipJoinAction vipJoinAction = vipJoinGroup.getGroup(player);
        if (vipJoinAction == null) {
            return;
        }
        vipJoinAction.performLeaveAction(player);
    }
}
